<template>
  <div class="login-container">
    <el-card shadow="hover">
      <!-- 登录标题 -->
      <div class="login-title"></div>
      <!-- 表单盒子 -->
      <el-form ref="form" status-icon :model="userInfo" :rules="rules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="userInfo.username" placeholder="请输入用户名" prefix-icon="el-icon-s-custom"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="userInfo.password"
            type="password"
            show-password
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <!-- 确认密码 -->
        <el-form-item prop="repassword">
          <el-input
            v-model="userInfo.repassword"
            type="password"
            show-password
            placeholder="请再次输入密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item>
          <el-button type="primary" @click="reg">注册</el-button>
        </el-form-item>
      </el-form>
      <!-- 登录下面超链接 -->
      <el-link :underline="false" @click="$router.push('/login')">已有账号，去登录</el-link>
      <!-- <router-link to="/login">已有账号，去登录</router-link> -->
    </el-card>
  </div>
</template>
<script>
import { regAPI } from '@/api/reg.js'
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.userInfo.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      userInfo: {
        username: '',
        password: '',
        repassword: ''
      },
      // 验证规则
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          // { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '用户名必须是1-10位数字字母', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ],

        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reg() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        // console.log('验证通过，可以提交了')
        const { data: res } = await regAPI(this.userInfo)
        if (res.code === 0) {
          // this.$message({ type: 'success', message: res.message })
          this.$message.success(res.message)
          // 清空输入框
          this.$refs.form.resetFields()
          // 跳转到登录页
          this.$router.push('/login')
        } else {
          this.$message({ type: 'error', message: res.message })
        }
      })
    }
  }
}
</script>
<style lang="less">
.login-container {
  height: 100%;
  background: url(../assets/images/login_bg.jpg) no-repeat;
  // 让图片居中沾满整个页面
  background-size: cover;
  .el-card {
    // height: 355px;
    width: 400px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .login-title {
      background: url(../assets/images/login_title.png) 50% no-repeat;
      height: 60px;
    }

    .el-button {
      width: 100%;
    }
  }
}
</style>
